package com.godofgamblers.hotelmanagementsystem.mapper;


import com.godofgamblers.hotelmanagementsystem.pojo.User;
import com.godofgamblers.hotelmanagementsystem.pojo.UserLevel;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 伴你时光尽头
 * @Date: 2019/10/18 15:39
 */
@Repository
public interface UserMapper {

    //通过输入的帐号,或手机号查询用户
    @Select("select * from user where user_account=#{user_account} or user_phone=#{user_phone} and flag=1")
    public User findUserByAccount_phone(User user);

    //通过帐号查询
    @Select("select * from user where user_account=#{user_account}  and flag=1")
    public  User findUserByUserAccount(String user_account);

    //通过手机号查询
    @Select("select * from user where user_phone=#{user_phone}  and flag=1")
    public  User findUserByUserPhone(String user_phone);

    //注册,添加
    @Insert("insert into user (user_name,user_account,user_password,user_phone) values(#{user_account},#{user_account},#{user_password},#{user_phone})")
    public  int register(User user);

    //通过帐号修改密码
    @Update("update user set user_password=#{user_password} where user_account=#{user_account} and flag=1")
    public  int updateByUser_Account(User user);

    @Insert("insert into user_role(user_id) values(#{user_id})")
    public int addUid(User user);
    /**
     * 查询：
     * 查询所有软删除状态为1存在的数据 封装成user类型的额集合返回  非用户
     *
     * @return user类型的额集合
     */
    @Select("SELECT * FROM `user`,(SELECT user_role.user_id,user_role.role_id,role.role_name FROM user_role,role WHERE user_role.role_id=role.role_id AND user_role.role_id != 0)r WHERE `user`.user_id=r.user_id  AND `user`.flag = 1")
    public List<User> showAllItem();

    /**
     * 查询：
     * 根据手机号码查询user
     * @param user  封装了user_phone的user对象
     * @return  查到的数据行各字段封装成的user对象
     */
    @Select("select * from `user` where user_phone = #{user_phone} and flag = 1")
    public User searchUser(User user);

    /**
     * 查询：
     * 根据账号查询user
     * @param user  封装了user_account的user对象
     * @return  查到的数据行各字段封装成的user对象
     */
    @Select("select * from `user` where user_account = #{user_account} and flag = 1")
    public User searchUserByAccount(User user);



/**
 * 用户表相关数据库操作
 */

    /**
     * 通过账号查询用户id
     * @param user_account  账号
     * @return  user_id
     */
    @Select("select user_id from user where user_account=#{user_account} and flag=1")
   public Integer findUser_idByAccount(String user_account);

    /**
     * 根据用户id查询用户积分
     * @param user_id   用户id
     * @return user_credit 积分
     */
    @Select("select user_credit from user where user_id=#{user_id} and flag=1")
    public int findUserCreditByUser_id(int user_id);

    /**
     * 修改用户积分
     * @param user   用户编号、修改后积分
     * @return  int 用户id
     */
    @Update("update user set user_credit=#{user_credit} where user_id=#{user_id} and flag=1")
    public int checkOutUpdateCridit(User user);

    @Select("SELECT COUNT(*) AS user_count,level_name\n" +
            "FROM `user` u,user_level ul\n" +
            "WHERE u.user_credit < ul.level_max_credit AND u.user_credit >= ul.level_min_credit\n" +
            "GROUP BY level_name")
    public List<UserLevel> getUserCount();

    /**
     * 查询用户信息
     * @param user_id
     * @return
     */
    @Select("select user_name,user_phone from `user` where user_id=#{user_id} and flag=1")
    User findUserinfoByUserId(Integer user_id);

    @Update("UPDATE user SET user_image=#{user_image} WHERE user_id=#{user_id} AND flag=1")
    void setHeadImage(User user);

    //传入用户修改用户的余额
    @Update("update user set user_wallet=#{user_wallet} where user_id=#{user_id} and flag=1")
    public void updateUserWallet(User user);

    //通过user_id查询到用户对象
    @Select("select * from user where user_id = #{user_id}")
    public User findUserByUserid(Integer user_id);

    @Select("SELECT user_image FROM user WHERE user_id = #{user_id} AND flag=1")
    public String findUser_ImageById(Integer user_id);

}
